package org.jeecg.modules.demo.basestaffstatusspecial.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.basejobserve.entity.BaseJobServe;
import org.jeecg.modules.demo.basejobserve.service.IBaseJobServeService;
import org.jeecg.modules.demo.basestaffstatus.entity.BaseStaffStatus;
import org.jeecg.modules.demo.basestaffstatus.service.IBaseStaffStatusService;
import org.jeecg.modules.demo.basestaffstatusspecial.entity.BaseStaffStatusSpecial;
import org.jeecg.modules.demo.basestaffstatusspecial.service.IBaseStaffStatusSpecialService;
import org.jeecg.modules.demo.commoditymanagement.entity.CommodityManagement;
import org.jeecg.modules.demo.commoditymanagement.service.ICommodityManagementService;
import org.jeecg.modules.demo.projectmanagement.entity.ProjectManagement;
import org.jeecg.modules.demo.projectmanagement.service.IProjectManagementService;
import org.jeecg.modules.util.RedisUtils;
import org.jeecg.modules.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 提成特殊设置
 * @Author: jeecg-boot
 * @Date: 2022-02-11
 * @Version: V1.0
 */
@Api(tags = "提成特殊设置")
@RestController
@RequestMapping("/basestaffstatusspecial/baseStaffStatusSpecial")
@Slf4j
public class BaseStaffStatusSpecialController extends JeecgController<BaseStaffStatusSpecial, IBaseStaffStatusSpecialService> {
    @Autowired
    private IBaseStaffStatusSpecialService baseStaffStatusSpecialService;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private ICommodityManagementService commodityManagementService;
    @Autowired
    private IProjectManagementService projectManagementService;
    @Autowired
    private IBaseJobServeService baseJobServeService;
    @Autowired
    private IBaseStaffStatusService baseStaffStatusService;

    /**
     * 分页列表查询（没用）
     */
    @AutoLog(value = "提成特殊设置-分页列表查询")
    @ApiOperation(value = "提成特殊设置-分页列表查询", notes = "提成特殊设置-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(BaseStaffStatusSpecial baseStaffStatusSpecial,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId + token);
        if (oConvertUtils.isEmpty(storeId)) {
            return Result.error("请先到首页选择店铺");
        }
        if (storeId.contains("过期")) {
            return Result.error("店铺已过期，续费后才可操作");
        }

        QueryWrapper<BaseStaffStatusSpecial> queryWrapper = new QueryWrapper<>();
        //queryWrapper.eq("store_id", storeId);
        QueryGenerator.initQueryWrapper(queryWrapper, baseStaffStatusSpecial, req.getParameterMap());
        Page<BaseStaffStatusSpecial> page = new Page<>(pageNo, pageSize);
        IPage<BaseStaffStatusSpecial> pageList = baseStaffStatusSpecialService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @AutoLog(value = "提成特殊设置-特殊条件查询")
    @ApiOperation(value = "提成特殊设置-特殊条件查询", notes = "提成特殊设置-特殊条件查询")
    @GetMapping(value = "/getRewardRate")
    public Result<?> getRewardRate(String staffStatusId, String type, String shopId) {
        QueryWrapper<BaseStaffStatusSpecial> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("staff_status_id", staffStatusId);
        queryWrapper.eq("type", type);
        queryWrapper.eq("shop_id", shopId);
        BaseStaffStatusSpecial bean = baseStaffStatusSpecialService.getOne(queryWrapper);
        return Result.OK(bean.getRewardRate());
    }

    /**
     * 根据groupId查询商品分组列表（用于入库时添加商品，查该分组的所有商品）
     */
    @GetMapping(value = "/getCommodityListByGroup")
    public Result<?> getCommodityListByGroup(String groupId, String staffStatusId, HttpServletRequest req) {
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId + token);
        if (oConvertUtils.isEmpty(storeId)) {
            return Result.error("请先到首页选择店铺");
        }
        if (storeId.contains("过期")) {
            return Result.error("店铺已过期，续费后才可操作");
        }
        List<CommodityManagement> list = commodityManagementService.list(new QueryWrapper<CommodityManagement>()
                .eq("group_id", groupId)
                .eq("store_id", storeId));
        List<Map<String, Object>> collect = list.stream().map(cm -> {
            Map<String, Object> map = ToolsUtils.objectToMap(cm);
            QueryWrapper<BaseStaffStatusSpecial> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("staff_status_id", staffStatusId);
            queryWrapper.eq("type", "1");
            queryWrapper.eq("shop_id", cm.getId());
            BaseStaffStatusSpecial bean = baseStaffStatusSpecialService.getOne(queryWrapper);
            if (bean == null) {
                BaseStaffStatus bss = baseStaffStatusService.getById(staffStatusId);
                map.put("rewardRate", bss.getShopReward());
            } else {
                map.put("rewardRate", bean.getRewardRate());
            }
            return map;
        }).collect(Collectors.toList());
        return Result.OK(collect);
    }

    /**
     * 根据groupId查询上架项目列表
     */
    @GetMapping(value = "/getProjectListByGroup")
    public Result<?> getProjectListByGroup(String groupId, String staffStatusId, HttpServletRequest req) {
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId + token);
        if (oConvertUtils.isEmpty(storeId)) {
            return Result.error("请先到首页选择店铺");
        }
        if (storeId.contains("过期")) {
            return Result.error("店铺已过期，续费后才可操作");
        }
        List<ProjectManagement> list = projectManagementService.list(new QueryWrapper<ProjectManagement>()
                .eq("group_id", groupId)
                .eq("store_id", storeId));
        List<Map<String, Object>> collect = list.stream().map(pm -> {
            Map<String, Object> map = ToolsUtils.objectToMap(pm);
            QueryWrapper<BaseStaffStatusSpecial> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("staff_status_id", staffStatusId);
            queryWrapper.eq("type", "2");
            queryWrapper.eq("shop_id", pm.getId());
            BaseStaffStatusSpecial bean = baseStaffStatusSpecialService.getOne(queryWrapper);
            if (bean == null) {
                BaseStaffStatus bss = baseStaffStatusService.getById(staffStatusId);
                map.put("rewardRate", bss.getProjectReward());
            } else {
                map.put("rewardRate", bean.getRewardRate());
            }
            return map;
        }).collect(Collectors.toList());
        return Result.OK(collect);
    }

    @GetMapping(value = "/getServeList")
    public Result<?> list(String staffStatusId, HttpServletRequest req) {
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId + token);
        if (oConvertUtils.isEmpty(storeId)) {
            return Result.error("请先到首页选择店铺");
        }
        if (storeId.contains("过期")) {
            return Result.error("店铺已过期，续费后才可操作");
        }
        List<BaseJobServe> list = baseJobServeService.list(new QueryWrapper<BaseJobServe>().eq("store_id", storeId));
        List<Map<String, Object>> collect = list.stream().map(bjs -> {
            Map<String, Object> map = ToolsUtils.objectToMap(bjs);
            QueryWrapper<BaseStaffStatusSpecial> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("staff_status_id", staffStatusId);
            queryWrapper.eq("type", "3");
            queryWrapper.eq("shop_id", bjs.getId());
            BaseStaffStatusSpecial bean = baseStaffStatusSpecialService.getOne(queryWrapper);
            if (bean == null) {
                BaseStaffStatus bss = baseStaffStatusService.getById(staffStatusId);
                map.put("rewardRate", bss.getServeReward());
            } else {
                map.put("rewardRate", bean.getRewardRate());
            }
            return map;
        }).collect(Collectors.toList());
        return Result.OK(collect);
    }

    /**
     * 添加
     *
     * @param baseStaffStatusSpecial
     * @return
     */
    @AutoLog(value = "提成特殊设置-添加")
    @ApiOperation(value = "提成特殊设置-添加", notes = "提成特殊设置-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody BaseStaffStatusSpecial baseStaffStatusSpecial) {
        QueryWrapper<BaseStaffStatusSpecial> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("staff_status_id", baseStaffStatusSpecial.getStaffStatusId());
        queryWrapper.eq("type", baseStaffStatusSpecial.getType());
        queryWrapper.eq("shop_id", baseStaffStatusSpecial.getShopId());
        BaseStaffStatusSpecial bean = baseStaffStatusSpecialService.getOne(queryWrapper);
        if (bean == null) {
            baseStaffStatusSpecialService.save(baseStaffStatusSpecial);
        } else {
            bean.setRewardRate(baseStaffStatusSpecial.getRewardRate());
            baseStaffStatusSpecialService.updateById(bean);
        }

        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param baseStaffStatusSpecial
     * @return
     */
    @AutoLog(value = "提成特殊设置-编辑")
    @ApiOperation(value = "提成特殊设置-编辑", notes = "提成特殊设置-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody BaseStaffStatusSpecial baseStaffStatusSpecial) {
        baseStaffStatusSpecialService.updateById(baseStaffStatusSpecial);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "提成特殊设置-通过id删除")
    @ApiOperation(value = "提成特殊设置-通过id删除", notes = "提成特殊设置-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        baseStaffStatusSpecialService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "提成特殊设置-批量删除")
    @ApiOperation(value = "提成特殊设置-批量删除", notes = "提成特殊设置-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.baseStaffStatusSpecialService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "提成特殊设置-通过id查询")
    @ApiOperation(value = "提成特殊设置-通过id查询", notes = "提成特殊设置-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        BaseStaffStatusSpecial baseStaffStatusSpecial = baseStaffStatusSpecialService.getById(id);
        if (baseStaffStatusSpecial == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(baseStaffStatusSpecial);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param baseStaffStatusSpecial
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BaseStaffStatusSpecial baseStaffStatusSpecial) {
        return super.exportXls(request, baseStaffStatusSpecial, BaseStaffStatusSpecial.class, "提成特殊设置");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BaseStaffStatusSpecial.class);
    }

}
